-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
updated big-endian handling: now completely host-independent #3
base: master
Are you sure you want to change the base?
Conversation
I've been giving this some though. I like the addition of |
I absolutely understand and agree on not reinventing the wheel when it's not the case. 100%. For the use case, I actually run it on a teensy 3.1 board where there is basically no system. 😄
But more generally, going towards C-only self sufficient code make it really universal and eternal, in always being able to build and run anywhere. |
just ran into this on an Arduino as well; this did the trick for me as a quick fix: #define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \
((x)<< 8 & 0x00FF0000UL) | \
((x)>> 8 & 0x0000FF00UL) | \
((x)>>24 & 0x000000FFUL) )
#define ntohl(x) htonl(x)
#define htonll(x) ( (uint64_t) htonl((uint32_t) x) << 32 | htonl(x>>32) )
#define ntohll(x) ntohll(x) |
Run into the same issue while building in a raspberry pi raspbian: I solved by suppressing -std=c99 option from build script as stated here: |
Just a heads up for anyone using this PR/Modifications: I found that floats and doubles are wrongly encoded/swapped due to missing casts. They have to be casted like they have been before using case 'f': {
if (i + 4 > len) return -3;
const float f = (float) va_arg(ap, double);
encode_uint32_t(*((uint32_t *) &f), buffer + i);
i += 4;
break;
}
case 'd': {
if (i + 8 > len) return -3;
const double f = (double) va_arg(ap, double);
encode_uint64_t(*((uint64_t *) &f), (buffer + i));
i += 8;
break;
} |
On Linux for Raspberry PI, I got the following message after running build.sh:
using this version of Linux:
It seems the the -std=c99 switch in build.sh prevents the __USE_MISC to be defined in endian.h. That makes htobe64(x) and be64toh(x) unavailable at compile time. I simply removed the -std-c99 switch and it seems to work. However, I don't know if there is any further considerations about removing this switch. |
Hi @lacasse4 @Paspartout @agnunez @s-ol, I just realized that I had never noticed, even once, within my Github notifications that your messages were about this PR of mine of 6 years ago, of which I had entirely forgotten. I will re-check everything this weekend and see what is the current scenario (as the PR has now even conflicts) Have a nice day you all. |
@mhroth is the project maintained, as should I work on the PR ? Thank you in advance for confirmation |
Hi @mhroth,
This commit removes the use of platform-specific functions (htonll, htobe64).
Luckily knowing the endianness of the transport is sufficient to be able to encode and decode without any knowledge of the host's architecture.
The substituting functions are short, readable, vanilla C, and will build and be correct on any platform and architecture.
( all inspired by this Rob Pike's article on endianness, functions included )
I find that readability results to be also slightly improved, but that is, of course, completely subjective.
Please feel free to return any feedback or demand for any change or modify it.
Cheers 👍